package com.edu.publisher.controller;

import com.edu.publisher.beans.TradeOrderProvinceAmount;
import com.edu.publisher.beans.TradeOrderProvinceCt;
import com.edu.publisher.beans.TradeOrderSourceAmount;
import com.edu.publisher.beans.TradeOrderSourceCt;
import com.edu.publisher.mapper.TradeStatsMapper;
import com.edu.publisher.service.TradeStatsService;
import com.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class TradeController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/provinceOrderCt")
    public String getOrderProvinceCt(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeOrderProvinceCt> tradeOrderProvinceCtList = tradeStatsService.getTradeOrderProvinceCt(date);
        if (tradeOrderProvinceCtList == null) {
            return "";
        }
        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeOrderProvinceCtList.size(); i++) {
            TradeOrderProvinceCt tradeOrderProvinceCt = tradeOrderProvinceCtList.get(i);
            String provinceId = tradeOrderProvinceCt.getProvinceId();
            Integer orderCt = tradeOrderProvinceCt.getOrderCt();
            mapData.append("{\n" +
                    "        \"name\": \"" + provinceId + "\",\n" +
                    "        \"value\": " + orderCt + "\n" +
                    "      }");
            if (i < tradeOrderProvinceCtList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"订单数\"\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/provinceOrderAmount")
    public String getOrderProvinceAmount(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeOrderProvinceAmount> tradeOrderProvinceAmountList = tradeStatsService.getTradeOrderProvinceAmount(date);
        if (tradeOrderProvinceAmountList == null) {
            return "";
        }
        StringBuilder mapData = new StringBuilder("[");
        for (int i = 0; i < tradeOrderProvinceAmountList.size(); i++) {
            TradeOrderProvinceAmount tradeOrderProvinceAmount = tradeOrderProvinceAmountList.get(i);
            String provinceId = tradeOrderProvinceAmount.getProvinceId();
            Double orderAmount = tradeOrderProvinceAmount.getOrderAmount();
            mapData.append("{\n" +
                    "        \"name\": \"" + provinceId + "\",\n" +
                    "        \"value\": " + orderAmount + "\n" +
                    "      }");
            if (i < tradeOrderProvinceAmountList.size() - 1) {
                mapData.append(",");
            } else {
                mapData.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"mapData\": " + mapData + ",\n" +
                "    \"valueName\": \"订单金额\"\n" +
                "  }\n" +
                "}";
    }



    @RequestMapping("/sourceOrderCt")
    public String getOrderSourceCt(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeOrderSourceCt> tradeOrderSourceCtList = tradeStatsService.getTradeOrderSourceCt(date);
        if (tradeOrderSourceCtList == null) {
            return "";
        }
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < tradeOrderSourceCtList.size(); i++) {
            TradeOrderSourceCt tradeOrderSourceCt = tradeOrderSourceCtList.get(i);
            String paymentType = tradeOrderSourceCt.getPaymentType();
            Integer orderCt = tradeOrderSourceCt.getOrderCt();
            rows.append("{\n" +
                    "        \"name\": \"" + paymentType + "\",\n" +
                    "        \"value\": " + orderCt + "\n" +
                    "      }");
            if (i < tradeOrderSourceCtList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"订单数\",\n" +
                "        \"id\": \"type\"\n" +
                "      },\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/sourceOrderAmount")
    public String getOrderSourceAmount(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeOrderSourceAmount> tradeOrderSourceAmountList = tradeStatsService.getTradeOrderSourceAmount(date);
        if (tradeOrderSourceAmountList == null) {
            return "";
        }
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < tradeOrderSourceAmountList.size(); i++) {
            TradeOrderSourceAmount tradeOrderSourceAmount = tradeOrderSourceAmountList.get(i);
            String paymentType = tradeOrderSourceAmount.getPaymentType();
            Double orderAmount = tradeOrderSourceAmount.getOrderAmount();
            rows.append("{\n" +
                    "        \"name\": \"" + paymentType + "\",\n" +
                    "        \"type\": " + orderAmount + "\n" +
                    "      }");
            if (i < tradeOrderSourceAmountList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"支付来源\",\n" +
                "        \"id\": \"name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"订单金额\",\n" +
                "        \"id\": \"type\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";
    }


}
